﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Client.Platform;
using Ebada.Platform.Model;

using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform.Org;
using Ebada.Client.Platform.Module;

namespace Ebada.Modules.Vehicle
{

    public partial class UCOrg2 : DevExpress.XtraEditors.XtraUserControl
    {
        public event SendDataEventHandler<mOrg> AfterAdd;
        public event SendDataEventHandler<mOrg> AfterEdit;
        public event SendDataEventHandler<mOrg> AfterDelete;
        GridViewOperation<mOrg> gridViewOperation;
        public event FocusedRowChangedEventHandler FocusedRowChanged;
        private string parentID = string.Empty;

        public UCOrg2()
        {
            InitializeComponent();
            gridViewOperation = new GridViewOperation<mOrg>(gridControl1, gridView1, barManager1 );
            gridViewOperation.CreatingObjectEvent += gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);
            gridViewOperation.AfterAdd += gridViewOperation_AfterAdd;
            gridViewOperation.AfterEdit += gridViewOperation_AfterEdit;
            gridViewOperation.AfterDelete += gridViewOperation_AfterDelete;

            this.btnOrg.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnOrg_ItemClick);
            this.btnTransport.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnTransport_ItemClick);
            this.btnCustomer.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnCustomer_ItemClick);
            this.btnInsurance.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnInsurance_ItemClick);
        }

        /// <summary>
        /// 发送添加事件
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_AfterAdd(mOrg newobj)
        {
            if (null != AfterAdd)
            {
                AfterAdd(null, newobj);
            }
        }

        /// <summary>
        /// 发送修改事件
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_AfterEdit(mOrg newobj)
        {
            if (null != AfterEdit)
            {
                AfterEdit(null, newobj);
            }
        }

        /// <summary>
        /// 发送删除事件
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_AfterDelete(mOrg newobj)
        {
            if (null != AfterDelete)
            {
                AfterDelete(null, newobj);
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.SetColumn(gridView1);
        }

        public void SetColumn(DevExpress.XtraGrid.Views.Grid.GridView grid)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in grid.Columns)
            {

                if (col.FieldName == "Org_ID")
                    col.Visible = false;

                if (col.FieldName == "OrgL_ID")
                    col.Visible = false;

                if (col.FieldName == "Sequence")
                    col.Visible = false;

                if (col.FieldName == "ParentID")
                    col.Visible = false;

                if (col.FieldName == "State")
                    col.Visible = false;

                if (col.FieldName == "OrgCode")
                {
                    col.Caption = "编号";
                    col.VisibleIndex = 0;
                }

                if (col.FieldName == "cnName")
                {
                    col.Caption = "中文名称";
                    col.VisibleIndex = 1;
                }

                if (col.FieldName == "cnFullName")
                {
                    col.Caption = "中文全名";
                    col.VisibleIndex = 2;
                }

                if (col.FieldName == "enName")
                {
                    col.Caption = "英文名称";
                    col.VisibleIndex = 3;
                }

                if (col.FieldName == "enFullName")
                {
                    col.Caption = "英文全名";
                    col.VisibleIndex = 4;
                }

                if (col.FieldName == "Postcode")
                {
                    col.Caption = "邮政编码";
                    col.VisibleIndex = 5;
                }

                if (col.FieldName == "Address")
                {
                    col.Caption = "地址";
                    col.VisibleIndex = 6;
                }

                if (col.FieldName == "Website")
                {
                    col.Caption = "网址";
                    col.VisibleIndex = 7;
                }

                if (col.FieldName == "Enabled")
                {
                    col.Caption = "启用";
                    col.VisibleIndex = 8;
                }

                if (col.FieldName == "Remark")
                {
                    col.Caption = "备注";
                    col.VisibleIndex = 9;
                }

                if (col.FieldName == "CityCode")
                {
                    col.Caption = "城市编码";
                    col.VisibleIndex = 10;
                }

                if (col.FieldName == "OurCompany")
                {
                    col.Caption = "公司概况";
                    col.VisibleIndex = 11;
                }

                if (col.FieldName == "RowVersion")
                {
                    col.Visible = false;
                }
            }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(mOrg newobj)
        {
            newobj.Org_ID = new mOrg().CreateID();
            //newobj.OrgL_ID = new mOrg().CreateID();
            //newobj.ParentID = parentID;
        }

        private mOrg editdata = null;
        /// <summary>
        /// 父表ID
        /// </summary>
        [Browsable(false)]
        public mOrg ParentData
        {
            set
            {
                editdata = value;

                if (null != value)
                {
                    parentID = value.Org_ID;
                    string str = string.Format(" WHERE parentid='{0}' ORDER BY cnName", parentID);
                    gridViewOperation.RefreshData(str);
                }

                else
                {
                    gridViewOperation.RefreshData("");
                }
            }
        }


        public void InnitonData(string where)
        {
            gridViewOperation.RefreshData(where); 
        }
        /// <summary>
        /// 是否显示工具条
        /// </summary>
        [Browsable(false)]
        public bool IsShowBarTools
        {
            set { this.barTools.Visible = value; }
        }

        /// <summary>
        /// 是否显示状态条
        /// </summary>
        [Browsable(false)]
        public bool IsShowBarStatus
        {
            set { this.barStatus.Visible = value; }
        }

        private mOrg NowItem()
        {
            mOrg org = this.gridView1.GetFocusedRow() as mOrg;

            return org;
        }

        /// <summary>
        /// 单位信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnOrg_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmOrgManager frmOrg = new frmOrgManager();

            if (null != this.NowItem())
                frmOrg.ShowOrg(this.NowItem().Org_ID);

            frmOrg.Text = "单位管理";
            frmOrg.MdiParent = MainHelper.MainForm;
            frmOrg.Show();
        }

        /// <summary>
        /// 客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnCustomer_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mOrg org = new mOrg();

            if (null != this.NowItem())
            {
                org.Org_ID = this.NowItem().Org_ID;

                frmOrgRelationManager frmORM = new frmOrgRelationManager(org);
                frmORM.ShowClientInfo();
            }
        }

        /// <summary>
        /// 承运商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnTransport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mOrg org = new mOrg();

            if (null != this.NowItem())
            {
                org.Org_ID = this.NowItem().Org_ID;

                frmOrgRelationManager frmORM = new frmOrgRelationManager(org);
                frmORM.ShowAgentInfo();
            }
        }

        /// <summary>
        /// 承保单位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnInsurance_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            mOrg org = new mOrg();

            if (null != this.NowItem())
            {
                org.Org_ID = this.NowItem().Org_ID;

                frmOrgRelationManager frmORM = new frmOrgRelationManager(org);
                frmORM.ShowInsureInfo();
            }
        }
    }
}
